# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# To build with Qt-Creator:
# 1. Launch Qt-Creator with the env (set from cmd.exe):
# set "MINGW_PREFIX=C:/msys64/mingw64"
# set "ACLOCAL_PATH=%MINGW_PREFIX%/share/aclocal:/usr/share/aclocal"
# set "PKG_CONFIG_PATH=%MINGW_PREFIX%/lib/pkgconfig"
# set "PATH=%MINGW_PREFIX%/bin;%PATH%"
# 2. Specify the following as the CMake arguments:
# -DMINGW_LIBS=%MINGW_PREFIX% -DCMAKE_BUILD_TYPE=Debug -DWITH_SYSTEM_GLEW=ON -DWITH_SYSTEM_OPENJPEG=ON -DWITH_INSTALL_PORTABLE=OFF -DWITH_OPENCOLLADA=ON -DWITH_GAMEENGINE=ON -DWITH_RAYOPTIMIZATION=ON -DWITH_JACK=OFF -DWITH_PLAYER=ON -DWITH_CODEC_FFMPEG=ON -DWITH_CODEC_SNDFILE=ON -DWITH_CYCLES=ON -DWITH_CYCLES_CUDA_BINARIES=OFF -DWITH_CYCLES_OSL=ON -DWITH_FFTW3=ON -DWITH_MOD_OCEANSIM=ON -DPYTHON_VERSION=3.7 -DPYTHON_LIBPATH=%MINGW_PREFIX%/lib -DPYTHON_LIBRARY="python3.6m" -DPYTHON_INCLUDE_DIRS=/msys64/mingw64/include/python3.6m -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_INSTALL_NUMPY=OFF -DPYTHON_NUMPY_PATH=%MINGW_PREFIX%/lib/python3.6/site-packages -DCYCLES_OSL=%MINGW_PREFIX% -DWITH_IMAGE_REDCODE=ON -DWITH_OPENMP=ON -DWITH_LLVM=ON -DLLVM_STATIC=ON

_realname=blender
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.93.11
pkgrel=6
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('GPL')
# buid conflict with "${MINGW_PACKAGE_PREFIX}-glog"
url="https://www.blender.org"
depends=("${MINGW_PACKAGE_PREFIX}-alembic"
         #"${MINGW_PACKAGE_PREFIX}-audaspace"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-embree"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         #"${MINGW_PACKAGE_PREFIX}-gflags"
         #"${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         $([[ "$CARCH" == "x86_64" ]] && echo "${MINGW_PACKAGE_PREFIX}-openimagedenoise")
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         #"${MINGW_PACKAGE_PREFIX}-opensubdiv"
         #$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-openvdb")
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-wintab-sdk"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ "$CARCH" == "aarch64" ]] && echo "${MINGW_PACKAGE_PREFIX}-sse2neon"))
install=blender-${MSYSTEM}.install
source=(https://download.blender.org/source/${_realname}-${pkgver}.tar.xz
        0001-mingw-buildsystem.patch
        0002-fix-mingw-w64-definitions.patch
        0004-fix-finding-glew.patch
        0005-fix-define-syntax.patch
        0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
        0008-mingw-use-X11-like-path-layout.patch
        0011-c++11-defines-__i386__-not-i386.patch
        0012-re-enable-SSE-on-mingw-w64-i686.patch
        0014-libstdc++-not-support-utf-stream.patch
        0015-mingw-python-like-msvc.patch
        0016-mingw-windres.patch
        0019-fix-casts.patch
        0020-blendthumb-build.patch
        0022-find-llvm-convert-paths.patch
        0023-link-errors.patch
        0024-openvdb-shared.patch
        0025-callback-function-prototype-fix.patch
        0026-embree-shared.patch
        0028-use-std-min-max.patch
        0029-update-CMake-to-support-OpenImageIO-2.3.4.patch
        0030-update-CMake-to-support-OpenEXR-3.patch
        0031-Images-update-code-to-support-OpenEXR-3.patch
        0032-fix-finding-tbb.patch
        0033-dont-add-fno-rtti-compile-option.patch
        0034-Fix-ARM64-Build.patch
        0035-Enable-SSE2NEON-on-Windows-ARM.patch
        0036-fix-building-against-boost-1.81.patch)
sha256sums=('7986110838e4a814c14c038aaaf4e517d2c0e46d076461eb66a2eecbfe7bdc58'
            '6f76d7cbfcbbb9fc823250bfbe9c8eea112aef33880a4665b2be19247048e449'
            'c301c9c06d11cc801d5c6d061cc42972e7a5ae770ffc7eca8fb84f60ae2d5050'
            'bcaa5b105463aa2dd9ab0f69ed4d422c23f9ae1a00a93e012c62113489f3db56'
            '87036a005a817bce9edd5fb7f18ba3b7a0b8d309d6f1f9bb0f813c931d889c0d'
            'cb603fd30a2e1421f653a187c3beb8e767d043a0d948213e884095cf0a27a411'
            '10d5d251132d2fd19d44e15249e8f80692ba613dd77cc8b7526cfdd5faa5a19b'
            '8774ed212ab365315e82b922c93b2018c90913bfcea8496205dee6d9e448b2a3'
            'f3e4a0cdec45a08df28f4d93dea508352df683d2fc8cf4b73f289db83eaf5d9e'
            '6c3e47c8051345bd7c98acdc8ea592889f650498793fd5053728cec4ac659303'
            'ed57c2bec0a48c2b946b48ea822874a69f578973c1334f8aefff51241db36c3d'
            '87684da9c11edd811ea939290bde8426e6aa5bb4003fe967f35b98055a60421c'
            '2f5967bbf0244fbd85e2fd7ffeb0b988bc5db3f9f520f7370cdd87b747f271ab'
            '23d7a41bff2d924a9232b78d39df1b8db79192c9e2940bb313daa18442cc0811'
            '8be734582b9980ad0a3c5a82c200767ce558084f18e3cca11947991a14b0d68a'
            '5a93bb201ab6bcff79b87a64cc931008f8911a3d4bfca9488d9b1f0d6fc0f3dc'
            'c39bfb8c5e4f6d26601e9cb03b979265ff0bbbcb2d2758005a4d0b0ed9616c7f'
            '61c0873f9c661f80eb189b7391a4c4c1eef2f7db4178177ce7ff7c47342384ae'
            '1e92e8be2e8e1c54df2608b064818a4ed0691a92b2d6c3066fe25a718fbf76b2'
            '26e296fa6b806282670a5688e8eaa334f5bab1dfb171d6f2fd37f9de5c574698'
            'ba7f6c1bfcf9c767225836f7874ea8d02675bd720a46946baf7e4c7a9459c72f'
            'fc46aab771d7791a0c97a50eb010cac452fa3c21f1c2299a1dfbbf9b4ba077c4'
            '25441ca67be1e8f2a2b792aa2c757620d08e4c98982b78a1cd3297f0ea422369'
            'd24fab2e66b6fa736923cbe765a52162880cdd71dce179f9aa50ec251955c31f'
            '16b560363717357a0e69d3b46269cace9bf978126e08b604ad18a532a8dfd0b2'
            '4eed05d38519354f83f70dd3bf3b88682948fc7c56ae998308f911312bb3306f'
            '9e4edfb6e7f19d0398e98017ce1fc3640d88002711dec5d49c8c46fbcfb5eb13'
            'dd70c472701632018c4e38b64697d8217506c7ab29182003f431031a16dac343')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  [[ -d build_files/cmake/platform/platform_win32_mingw64.cmake ]] && rm build_files/cmake/platform/platform_win32_mingw64.cmake

  apply_patch_with_msg \
    0001-mingw-buildsystem.patch \
    0002-fix-mingw-w64-definitions.patch \
    0004-fix-finding-glew.patch \
    0005-fix-define-syntax.patch \
    0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch \
    0008-mingw-use-X11-like-path-layout.patch \
    0011-c++11-defines-__i386__-not-i386.patch \
    0012-re-enable-SSE-on-mingw-w64-i686.patch \
    0014-libstdc++-not-support-utf-stream.patch \
    0015-mingw-python-like-msvc.patch \
    0016-mingw-windres.patch \
    0019-fix-casts.patch \
    0020-blendthumb-build.patch \
    0022-find-llvm-convert-paths.patch \
    0023-link-errors.patch \
    0024-openvdb-shared.patch \
    0025-callback-function-prototype-fix.patch \
    0026-embree-shared.patch \
    0028-use-std-min-max.patch \
    0029-update-CMake-to-support-OpenImageIO-2.3.4.patch \
    0030-update-CMake-to-support-OpenEXR-3.patch \
    0031-Images-update-code-to-support-OpenEXR-3.patch \
    0032-fix-finding-tbb.patch \
    0033-dont-add-fno-rtti-compile-option.patch \
    0034-Fix-ARM64-Build.patch \
    0035-Enable-SSE2NEON-on-Windows-ARM.patch \
    0036-fix-building-against-boost-1.81.patch
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir "${srcdir}"/build-${MSYSTEM}  && cd "${srcdir}"/build-${MSYSTEM}

  # i686 SSE is problematic with Cycles: https://developer.blender.org/T36316
  # .. but commit:
  # https://lists.blender.org/pipermail/bf-blender-cvs/2013-October/059705.html
  # .. breaks the build:
  # undefined reference to `ccl::kernel_cpu_sse2_path_trace` .. etc
  # This is because it prevents the definition of __KERNEL_SSE2__ without having
  # ensured WITH_CYCLES_OPTIMIZED_KERNEL_SSE2 is undefined. Let's hope that the
  # bugs have been fixed as patch 0011 re-enables __KERNEL_SSE2__. If they're not
  # fixed then we'll have to pass ENABLESSE2 to cmake instead, -DWITH_CPU_SSE being
  # the correct one (probably!)
  # [[ $CARCH == i686 ]] && ENABLESSE2="-DSUPPORT_SSE2_BUILD=OFF -DWITH_CPU_SSE=OFF"
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing -Wno-array-parameter -Wno-unreachable-code-generic-assoc"
  else
    CXXFLAGS+=" -Wno-narrowing -Wno-stringop-truncation -Wno-array-parameter"
  fi

  if [[ $CARCH == "aarch64" ]]; then
    _sse2neon="${MINGW_PREFIX}/include/sse2neon"
  else
    _sse2neon=""
  fi

  export XR_OPENXR_SDK_ROOT_DIR=${MINGW_PREFIX}
  export EMBREE_ROOT_DIR=${MINGW_PREFIX}

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMINGW_LIBS=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DWITH_SYSTEM_AUDASPACE=OFF \
    -DWITH_SYSTEM_GLEW=ON \
    -DWITH_SYSTEM_GFLAGS=OFF \
    -DWITH_SYSTEM_GLOG=OFF \
    -DWITH_SYSTEM_BULLET=OFF \
    -DWITH_SYSTEM_LZO=ON \
    -DWITH_SYSTEM_EIGEN3=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENCOLLADA=ON \
    -DWITH_ALEMBIC=ON \
    -DWITH_JACK=OFF \
    -DWITH_CODEC_FFMPEG=ON \
    -DWITH_CODEC_SNDFILE=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_STANDALONE=ON \
    -DWITH_CYCLES_STANDALONE_GUI=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_FFTW3=ON \
    -DWITH_OPENVDB=OFF \
    -DWITH_MEM_JEMALLOC=OFF \
    -DWITH_MOD_OCEANSIM=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DPython_ROOT_DIR=${MINGW_PREFIX} \
    -DPython_FIND_REGISTRY=NEVER \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=${MINGW_PREFIX}/lib/libpython${_pyver}.dll.a \
    -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver} \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DCYCLES_OSL=${MINGW_PREFIX} \
    -DWITH_OPENMP=ON \
    -DWITH_LLVM=ON \
    -DLLVM_STATIC=ON \
    -DSSE2NEON_ROOT_DIR=${_sse2neon} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}"
  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q \
    ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/startup \
    ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/modules \
    ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/addons
}
